/**
Copyright [2009] [Julian Gamble] 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
 **/
package cruiseprojectlist.service;

import java.util.Map;

import cruiseprojectlist.domain.ProjectPreference;

public class ProjectPreferencesService {
	protected static final String PROJECT_PREFERENCES_XML = "ProjectPreferences.xml";

	private static ProjectPreferencesService instance = null;

	private Map<String, ProjectPreference> projectPreferencesMap = null;

	protected ProjectPreferencesService() {
		// Exists only to defeat instantiation.
		initProjectPreferences();
	}

	
	private void initProjectPreferences() {
		projectPreferencesMap = ProjectStorageBucketService.getProjectPreferencesMap();
		/*if (new File(PROJECT_PREFERENCES_XML).exists()) {
			readProjectPreferences();
		} else {
			projectPreferencesMap = new TreeMap<String, ProjectPreference>();
		}*/

	}
	
	/*@SuppressWarnings("unchecked")
	private void readProjectPreferences() {
		XMLDecoder d = null;
		try {
			d = new XMLDecoder(new BufferedInputStream(new FileInputStream(
					PROJECT_PREFERENCES_XML)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		Object objectReadResult = d.readObject();
		projectPreferencesMap = (Map<String, ProjectPreference>) objectReadResult;
		d.close();
	}*/

	public static ProjectPreferencesService getInstance() {
		if (instance == null) {
			instance = new ProjectPreferencesService();
		}
		return instance;
	}

	public ProjectPreference getProjectPreferences(String projectName) {
		refreshProjectPreferencesMap();
		ProjectPreference result = projectPreferencesMap.get(projectName);
		if (result==null) {
			result = new ProjectPreference();
		}
		return result;
	}

	/*public void saveProjectPreference(String projectName,
			ProjectPreference projectPreferences) {
		cleanOldProject(projectName);
		persistNewProject(projectName, projectPreferences);
	}*/


	/*private void persistNewProject(String projectName,
			ProjectPreference projectPreferences) {
		// Put the new one in
		projectPreferencesMap.put(projectName, projectPreferences);
		XMLEncoder e = null;
		try {
			e = new XMLEncoder(new BufferedOutputStream(
					new FileOutputStream(PROJECT_PREFERENCES_XML)));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		
		e.writeObject(projectPreferencesMap);
		e.close();
	}*/


	private void refreshProjectPreferencesMap() {
		projectPreferencesMap = ProjectStorageBucketService.getProjectPreferencesMap();
	}


	/*private void cleanOldProject(String projectName) {
		// If it exists - replace it
		ProjectPreference oldProjectPreference = projectPreferencesMap
				.get(projectName);
		if (oldProjectPreference != null)
			projectPreferencesMap.remove(projectName);
	}*/

}
